#define  _CRT_SECURE_NO_WARNINGS
#include "DeclareFile.h"


void menu() {
    cleardevice();

    outtextxy(0, 0, "菜单");
}


BUTTON* createbutton(int x, int y, int width, int height, COLORREF curColor, COLORREF inColor, COLORREF outColor, const char* text) {

    BUTTON* pb = (BUTTON*)malloc(sizeof(BUTTON));
    assert(pb);
    pb->x = x;
    pb->y = y;
    pb->width = width;
    pb->height = height;
    pb->curColor = outColor;
    pb->inColor = inColor;
    pb->outColor = outColor;

    int lenght = strlen(text) + 1;
    pb->text = (char*)malloc(sizeof(char) * lenght);
    assert(pb->text);
    strcpy_s(pb->text, lenght, text);

    return pb;
}


void drewbutton(BUTTON* pb) {
    setfillcolor(pb->curColor);
    fillrectangle(pb->x, pb->y, pb->x + pb->width, pb->y - pb->height);


    settextcolor(BLACK);
    setbkmode(TRANSPARENT);
    settextstyle(30, 0, "微软雅黑");

    int textw = textwidth(pb->text);           //文字居中
    int texth = textheight(pb->text);
    int tx = pb->x + (pb->width - textw) / 2;
    int ty = pb->y - (pb->height + texth) / 2;

    outtextxy(tx, ty, pb->text);

}


int isinbutton(BUTTON* pb, ExMessage m) {
    if (m.x > pb->x && m.x < pb->x + pb->width && m.y + 50> pb->y && m.y + 50 < pb->y + pb->height) {
        pb->curColor = pb->inColor;
        return 1;
    }
    pb->curColor = pb->outColor;
    return 0;
}

int isclikbutton(BUTTON* pb, ExMessage m) {
    if (isinbutton(pb, m) && m.message == WM_LBUTTONDOWN) {
        return 1;
    }
    return 0;
}

void GameEnd() {
    setbkcolor(WHITE);
    cleardevice();
    putimage(0, 0, &BgImg);

    //存储  转换为字符串的数字
    char str[20];
    sprintf(str, "%d", score);

    settextcolor(BLACK);
    settextstyle(30, 0, "微软雅黑");
    outtextxy(300, 100, "分数：");
    outtextxy(400, 100, str);

    setlinecolor(DARKGRAY);
    rectangle(300, 150, 500, 200);

    BUTTON* Return = createbutton(330, 300, 130, 50, RGB(153, 217, 234), RGB(112, 146, 190),
        RGB(153, 217, 234), "返回菜单");

    BeginBatchDraw(); // 双缓冲3句
    int showbutton = 1;
    ExMessage m;
    while (1) {
        if (showbutton) drewbutton(Return);
        if (peekmessage(&m, EM_MOUSE)) {
            if (isclikbutton(Return, m)) {
                menu();
                showbutton = 0;
            }
        }
        FlushBatchDraw();
    }
    EndBatchDraw();

    closegraph();
}